package com.pachong.android.frameworkbase.dialog;

import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pachong.android.frameworkbase.R;
import com.pachong.android.frameworkbase.dialog.base.CenterDialogFragment;

/**
 * Alert Dialog Fragment
 *
 * @Author: PengWeiwei
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2015-05-04
 * @Version: 1.0.0
 */
public class AlertDialogFragment extends CenterDialogFragment {

    protected TextView mTvTitle;
    protected TextView mTvMessage;
    protected Button mBtnNegative;
    protected Button mBtnPositive;
    protected LinearLayout dialogLytCenterContainer;

    protected CharSequence mTitle;
    protected CharSequence mMessage;
    protected CharSequence mNegativeBtnText;
    protected CharSequence mPositiveBtnText;


    @StringRes
    protected int mTitleId;

    @StringRes
    protected int mMessageId;

    @StringRes
    protected int mNegativeBtnTextId;

    @StringRes
    protected int mPositiveBtnTextId;

    protected View.OnClickListener mNegativeClickedListener;
    protected View.OnClickListener mPositiveClickedListener;
    protected View mLineTiTle;
    protected View mLineMessage;
    protected View mLinePosNeg;

    /**
     * 标记哪个view需要request focus
     */
    private int mRequestFocusViewTag;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), null);
        mTvTitle = (TextView) view.findViewById(R.id.dialogTvTitle);
        mTvMessage = (TextView) view.findViewById(R.id.dialogTvMessage);
        mBtnNegative = (Button) view.findViewById(R.id.dialogBtnNegative);
        mBtnPositive = (Button) view.findViewById(R.id.dialogBtnPositive);
        dialogLytCenterContainer = (LinearLayout) view.findViewById(R.id.dialogLytCenterContainer);

        mLineTiTle = view.findViewById(R.id.dialogTitleLine);
        mLineMessage = view.findViewById(R.id.dialogMessageLine);
        //消极-积极按钮分割线
        mLinePosNeg = view.findViewById(R.id.dialogButtonDivider);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!TextUtils.isEmpty(getTitle())) {
            mTvTitle.setText(getTitle());
            mTvTitle.setVisibility(View.VISIBLE);
            //Title与Message分割线
            mLineTiTle.setVisibility(View.VISIBLE);
        }

        if (!TextUtils.isEmpty(getMessage())) {
            mTvMessage.setText(getMessage());
            mTvMessage.setVisibility(View.VISIBLE);
            mLineMessage.setVisibility(View.VISIBLE);
        }

        if (!TextUtils.isEmpty(getNegativeBtnText())) {
            mBtnNegative.setText(getNegativeBtnText());
            mBtnNegative.setOnClickListener(mNegativeClickedListener);
            mBtnNegative.setVisibility(View.VISIBLE);
            mLinePosNeg.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(getPositiveBtnText())) {
            mBtnPositive.setText(getPositiveBtnText());
            mBtnPositive.setOnClickListener(mPositiveClickedListener);
            mBtnPositive.setVisibility(View.VISIBLE);
            mLinePosNeg.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(getNegativeBtnText()) && !TextUtils.isEmpty(getPositiveBtnText())) {
            mLinePosNeg.setVisibility(View.VISIBLE);
        }

        if (mRequestFocusViewTag > 0) {
            View focusView = view.findViewById(mRequestFocusViewTag);
            focusView.setFocusable(true);
            focusView.setFocusableInTouchMode(true);
            focusView.requestFocus();
        }


    }

    @LayoutRes
    int getLayoutId() {
        TypedArray typedArray1 = getActivity().obtainStyledAttributes(null, R.styleable.PcfAlertDialog, R.attr.pcfAlertDialogStyle, 0);
        int layoutId = typedArray1.getResourceId(R.styleable.PcfAlertDialog_android_layout, -1);
        if (layoutId < 0) {
            layoutId = R.layout.fwb_fragment_alert_dialog;
        }
        typedArray1.recycle();
//        return layoutId;
        return R.layout.fwb_fragment_alert_dialog;
    }


    public AlertDialogFragment setTitle(CharSequence title) {
        mTitle = title;
        return this;
    }

    public AlertDialogFragment setTitle(@StringRes int titleRes) {
        mTitleId = titleRes;
        return this;
    }

    public AlertDialogFragment setMessage(CharSequence msg) {
        mMessage = msg;
        return this;
    }

    public AlertDialogFragment setMessage(@StringRes int msgRes) {
        mMessageId = msgRes;
        return this;
    }


    public AlertDialogFragment setNegativeButton(CharSequence text, View.OnClickListener listener) {
        return setNegativeButton(text, false, listener);
    }

    public AlertDialogFragment setNegativeButton(CharSequence text, boolean requestFocus, View.OnClickListener listener) {
        mNegativeClickedListener = listener;
        mNegativeBtnText = text;
        if (requestFocus) {
            mRequestFocusViewTag = R.id.dialogBtnNegative;
        }
        return this;
    }

    public AlertDialogFragment setNegativeButton(@StringRes int strId, View.OnClickListener listener) {
        return setNegativeButton(strId, false, listener);
    }

    public AlertDialogFragment setNegativeButton(@StringRes int strId, boolean requestFocus, View.OnClickListener listener) {
        mNegativeClickedListener = listener;
        mNegativeBtnTextId = strId;
        if (requestFocus) {
            mRequestFocusViewTag = R.id.dialogBtnNegative;
        }
        return this;
    }

    public AlertDialogFragment setPositiveButton(CharSequence text, View.OnClickListener listener) {
        return setPositiveButton(text, false, listener);
    }

    public AlertDialogFragment setPositiveButton(CharSequence text, boolean requestFocus, View.OnClickListener listener) {
        mPositiveClickedListener = listener;
        mPositiveBtnText = text;
        if (requestFocus) {
            mRequestFocusViewTag = R.id.dialogBtnPositive;
        }
        return this;
    }

    public AlertDialogFragment setPositiveButton(@StringRes int strId, View.OnClickListener listener) {
        return setPositiveButton(strId, false, listener);
    }

    public AlertDialogFragment setPositiveButton(@StringRes int strId, boolean requestFocus, View.OnClickListener listener) {
        mPositiveClickedListener = listener;
        mPositiveBtnTextId = strId;
        if (requestFocus) {
            mRequestFocusViewTag = R.id.dialogBtnPositive;
        }
        return this;
    }


    public CharSequence getTitle() {
        if (mTitleId > 0) {
            return getString(mTitleId);
        } else {
            return mTitle;
        }
    }

    public CharSequence getMessage() {
        if (mMessageId > 0) {
            return getString(mMessageId);
        } else {
            return mMessage;
        }
    }

    public CharSequence getNegativeBtnText() {
        if (mNegativeBtnTextId > 0) {
            return getString(mNegativeBtnTextId);
        } else {
            return mNegativeBtnText;
        }
    }

    public CharSequence getPositiveBtnText() {
        if (mPositiveBtnTextId > 0) {
            return getString(mPositiveBtnTextId);
        } else {
            return mPositiveBtnText;
        }
    }


    /**
     * should be null
     *
     * @return
     */
    public Button getNegativeButton() {
        return mBtnNegative;
    }

    /**
     * should be null
     *
     * @return
     */
    public Button getPositiveButton() {
        return mBtnPositive;
    }


}
